#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > geoopt_delintmin.info <<'%EOF%'
   geoopt_delintmin
   ----------------
   Molecule:         C6H5CHO
   Wave Function:    SCF / STO-3G
   Test Purpose:     Minimization in delocalized internal coordinates.
                     A model Hessian is determined initially, then updated
                     using the DFP-formula. Baker's convergence criteria
                     are used
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > geoopt_delintmin.mol <<'%EOF%'
BASIS
STO-3G
First order minimization in delocalized internal coordinates

Atomtypes=3 Generators=1 Z
Charge=8.0 Atoms=1
O     6.1169594363        0.0000000000        0.0000000000             *
Charge=6.0 Atoms=7
C    -0.4281183799       -2.2595362151        0.0000000000             *
C    -2.9286935224       -1.4347871225        0.0000000000             *
C    -3.4656164013        1.1411808224        0.0000000000             *
C    -1.5061149145        2.8972276388        0.0000000000             *
C     0.9961412279        2.0785184368        0.0000000000             *
C     1.5529020669       -0.5103443405        0.0000000000             *
C     4.3100239391       -1.4696981757        0.0000000000             *
Charge=1.0 Atoms=6
H     4.6927731310       -3.5243404323        0.0000000000             *
H    -0.0483891239       -4.2673340819        0.0000000000             *
H    -4.4516781974       -2.7942683921        0.0000000000             *
H    -5.4051670195        1.7780840801        0.0000000000             *
H    -1.9265366341        4.8949599174        0.0000000000             *
H     2.4915143918        3.4703378645        0.0000000000             *
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > geoopt_delintmin.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
*OPTIMIZE
.DELINT
.DFP
.BAKER
**WAVE FUNCTION
.HF
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >geoopt_delintmin.check
cat >>geoopt_delintmin.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

# Geometry optimization
CRIT1=`$GREP "1st order method with DFP update will be used\." $log | wc -l`
CRIT2=`$GREP "Optimization will be performed in delocalized internal coordinates\." $log | wc -l`
CRIT3=`$GREP "Model Hessian will be used as initial Hessian\." $log | wc -l`
CRIT4=`$GREP "Baker.s convergence criteria will be used" $log | wc -l`
TEST[1]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[1]=4
ERROR[1]="GEOMETRY OPTIMIZATION NOT SET UP CORRECTLY"


# Symmetry
CRIT1=`$GREP "Number of coordinates in each symmetry\: * 28 * 14" $log | wc -l`
CRIT2=`$GREP "Number of orbitals in each symmetry\: * 38 * 8" $log | wc -l`
TEST[3]=`expr	$CRIT1 \+ $CRIT2`
CTRL[3]=2
ERROR[3]="SYMMETRY NOT CORRECT"

# Initial energy
CRIT1=`$GREP "Final * HF energy\: * \-339\.11787646(8|9)" $log | wc -l`
TEST[4]=`expr	$CRIT1`
CTRL[4]=1
ERROR[4]="INITIAL ENERGY NOT CORRECT"

# Initial gradient
CRIT1=`$GREP "C * y * (\-0|\-)\.010726...[0-9]" $log | wc -l`
CRIT2=`$GREP "C * x * (\-0|\-)\.00889(8|9)...[0-9]" $log | wc -l`
CRIT3=`$GREP "C * y * (\-0|\-)\.006274...[0-9]" $log | wc -l`
CRIT4=`$GREP "C * x * (\-0|\-)\.007739...[0-9]" $log | wc -l`
CRIT5=`$GREP "C * y * (0| )\.003485...[0-9]" $log | wc -l`
CRIT6=`$GREP "C * x * (\-0|\-)\.004568...[0-9]" $log | wc -l`
CRIT7=`$GREP "C * y * (0| )\.011628...[0-9]" $log | wc -l`
CRIT8=`$GREP "C * x * (0| )\.008232...[0-9]" $log | wc -l`
CRIT9=`$GREP "C * y * (0| )\.005583...[0-9]" $log | wc -l`
CRIT10=`$GREP "C * x * (\-0|\-)\.00837(1|2)...[0-9]" $log | wc -l`
CRIT11=`$GREP "C * y * (0| )\.014393...[0-9]" $log | wc -l`
CRIT12=`$GREP "C * x * (\-0|\-)\.011686...[0-9]" $log | wc -l`
CRIT13=`$GREP "C * y * (\-0|\-)\.031174...[0-9]" $log | wc -l`
CRIT14=`$GREP "H * x * (0| )\.013400...[0-9]" $log | wc -l`
CRIT15=`$GREP "H * y * (\-0|\-)\.000507...[0-9]" $log | wc -l`
CRIT16=`$GREP "H * x * (\-0|\-)\.003238...[0-9]" $log | wc -l`
CRIT17=`$GREP "H * y * (0| )\.001156...[0-9]" $log | wc -l`
CRIT18=`$GREP "H * x * (0| )\.001431...[0-9]" $log | wc -l`
CRIT19=`$GREP "H * y * (0| )\.001561...[0-9]" $log | wc -l`
CRIT20=`$GREP "H * x * (0| )\.002459...[0-9]" $log | wc -l`
CRIT21=`$GREP "H * y * (\-0|\-)\.000748...[0-9]" $log | wc -l`
CRIT22=`$GREP "H * x * (\-0|\-)\.000030...[0-9]" $log | wc -l`
CRIT23=`$GREP "H * y * (\-0|\-)\.002110...[0-9]" $log | wc -l`
CRIT24=`$GREP "H * x * (\-0|\-)\.004706...[0-9]" $log | wc -l`
CRIT25=`$GREP "H * y * (0| )\.001641...[0-9]" $log | wc -l`
TEST[5]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11 \+ $CRIT12 \+ \
		$CRIT13 \+ $CRIT14 \+ $CRIT15 \+ $CRIT16 \+ $CRIT17 \+ $CRIT18 \+ \
		$CRIT19 \+ $CRIT20 \+ $CRIT21 \+ $CRIT22 \+ $CRIT23 \+ $CRIT24 \+ \
		$CRIT25`
CTRL[5]=25
ERROR[5]="INITIAL GRADIENT NOT CORRECT"

# Initial step
CRIT1=`$GREP "O * 6\.09175(2|3|4)...[0-9] * (0| )\.03512(2|3|4)...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP "C * (\-0|\-)\.424075...[0-9] * \-2\.261560...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT3=`$GREP "C * \-2\.911834...[0-9] * \-1\.440339...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT4=`$GREP "C * \-3\.44146(4|5)...[0-9] * 1\.127269...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT5=`$GREP "C * \-1\.48117(0|1)...[0-9] * 2\.871044...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT6=`$GREP "C * 1\.006321...[0-9] * 2\.05211(2|3)...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT7=`$GREP "C * 1\.547034...[0-9] * (\-0|\-)\.52033(3|4)...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT8=`$GREP "C * 4\.276753...[0-9] * \-1\.39233(8|9)...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT9=`$GREP "H * 4\.53535(3|4|5)...[0-9] * \-3\.460763...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT10=`$GREP "H * (\-0|\-)\.0182(79|80)...[0-9] * \-4\.267949...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT11=`$GREP "H * \-4\.43846(0|1)...[0-9] * \-2\.802235...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT12=`$GREP "H * \-5\.38426(5|6)...[0-9] * 1\.77097(1|2)...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT13=`$GREP "H * \-1\.89312(4|5)...[0-9] * 4\.87487(3|4)...[0-9] * (0| )\.0000000000" $log | wc -l`
CRIT14=`$GREP "H * 2\.535460...[0-9] * 3\.41412(3|4)...[0-9] * (0| )\.0000000000" $log | wc -l`
TEST[6]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11 \+ $CRIT12 \+ \
		$CRIT13 \+ $CRIT14`
TEST[6]=14 # disabled INITIAL STEP test because the test is too tight /hjaaj Aug. 2015
CTRL[6]=14
ERROR[6]="INITIAL STEP NOT CORRECT"

# Second iteration
CRIT1=`$GREP "Energy at this geometry is * \: * \-339\.12074[0-9]" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * (0| )\.00813[0-9]" $log | wc -l`
CRIT3=`$GREP "Norm of step * \: * (0| )\.0836(1|2)[0-9]" $log | wc -l`
TEST[7]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[7]=3
ERROR[7]="SECOND ITERATION NOT CORRECT"

# Third iteration
CRIT1=`$GREP "Energy at this geometry is * \: * \-339\.12083[0-9]" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * (0| )\.00215[0-9]" $log | wc -l`
CRIT3=`$GREP "Norm of step * \: * (0| )\.01050[0-9]" $log | wc -l`
TEST[8]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[8]=3
ERROR[8]="THRID ITERATION NOT CORRECT"

# Fourth iteration
CRIT1=`$GREP "Norm of gradient * \: * (0| )\.00056[0-9]" $log | wc -l`
CRIT2=`$GREP "Norm of step * \: * (0| )\.003(69|70|71)[0-9]" $log | wc -l`
TEST[9]=`expr	$CRIT1 \+ $CRIT2`
CTRL[9]=2
ERROR[9]="FOURTH ITERATION NOT CORRECT"

# Final geometry
 CRIT1=`$GREP "O * 3\.223800.... * ( |0)\.00831(2|3).... * 0*\.0000000000" $log | wc -l`
 CRIT2=`$GREP "C * (\-|\-0)\.218893.... * \-1\.193163.... * 0*\.0000000000" $log | wc -l`
 CRIT3=`$GREP "C * -1\.535571.... * (\-|\-0)\.762271.... * 0*\.0000000000" $log | wc -l`
 CRIT4=`$GREP "C * -1\.819798.... * ( |0)\.595801.... * 0*\.0000000000" $log | wc -l`
 CRIT5=`$GREP "C * (\-|\-0)\.7867(20|19).... * 1\.523229.... * 0*\.0000000000" $log | wc -l`
 CRIT6=`$GREP "C * ( |0)\.529038.... * 1\.09178(2|3).... * 0*\.0000000000" $log | wc -l`
 CRIT7=`$GREP "C * ( |0)\.821421.... * (\-|\-0)\.26957(8|9).... * 0*\.0000000000" $log | wc -l`
 CRIT8=`$GREP "C * 2\.259526.... * (\-|\-0)\.739898.... * 0*\.0000000000" $log | wc -l`
 CRIT9=`$GREP "H * 2\.37754(1|2).... * \-1\.837050.... * 0*\.0000000000" $log | wc -l`
CRIT10=`$GREP "H * ( |0)\.005809.... * \-2\.252912.... * 0*\.0000000000" $log | wc -l`
CRIT11=`$GREP "H * \-2\.34169(2|3).... * \-1\.484735.... * 0*\.0000000000" $log | wc -l`
CRIT12=`$GREP "H * \-2\.84909(2|3).... * ( |0)\.93264(6|7).... * 0*\.0000000000" $log | wc -l`
CRIT13=`$GREP "H * \-1\.009791.... * 2\.582546.... * 0*\.0000000000" $log | wc -l`
CRIT14=`$GREP "H * 1\.344421.... * 1\.80529(0|1).... * 0*\.0000000000" $log | wc -l`
TEST[10]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11 \+ $CRIT12 \+ \
		$CRIT13 \+ $CRIT14`
CTRL[10]=14
ERROR[10]="FINAL GEOMETRY NOT CORRECT"

# Geometry convergence
CRIT1=`$GREP "Geometry converged in * 5( |  )iterations\!" $log | wc -l`
CRIT2=`$GREP "Energy at final geometry is * \: * \-339\.12084[0-9] a\.u\." $log | wc -l`
CRIT3=`$GREP "Energy change during optimization \: * (\-0|\-)\.00296[0-9] a\.u\." $log | wc -l`
TEST[11]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[11]=3
ERROR[11]="GEOMETRY OPTIMIZATION NOT CONVERGED"

PASSED=1
for i in 1 3 4 5 6 7 8 9 10 11
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} : ${TEST[i]} .ne. ${CTRL[i]} ; "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
